/*
* @Author: dyiwen
* @Date:   2020-09-21 14:06:52
* @Last Modified by:   dyiwen
* @Last Modified time: 2020-09-22 15:58:16
 */
package main

import (
	// "fmt"
	"github.com/urfave/cli"
	"os"
	"sort"
)

func init_cmd() {
	app := cli.NewApp()
	app.Name = "harborctl"
	app.HelpName = "harborctl"
	app.Usage = "运维使用的harbor管理工具"
	app.Version = "1.0.0"
	app.Author = "duanyiwen"

	app.Commands = []cli.Command{
		{
			Name: "get",
			// Aliases:  []string{"a"},
			Usage:    "查询",
			Category: "Basic Commands (Intermediate)",
			// Action:   getHandler,
			Subcommands: []cli.Command{
				{
					Name:   "project",
					Usage:  "查询project",
					Action: getProjectHandler,
				},
				{
					Name:   "repo",
					Usage:  "查询repository",
					Action: getRepoHandler,
					Flags: []cli.Flag{
						cli.StringFlag{
							Name:     "project, p",
							Usage:    "指定项目名称",
							Required: true,
						},
						cli.Int64Flag{
							Name:        "pagesize, s",
							Usage:       "指定容量",
							Value:       20,
							Destination: &page_size,
						},
						cli.Int64Flag{
							Name:        "page, i",
							Usage:       "指定页码",
							Value:       1,
							Destination: &page,
						},
					},
				},
				{
					Name:   "image",
					Usage:  "查询image",
					Action: getImageHandler,
					Flags: []cli.Flag{
						cli.StringFlag{
							Name:        "project, p",
							Usage:       "指定项目名称",
							Required:    true,
							Destination: &project,
						},
						cli.Int64Flag{
							Name:        "pagesize, s",
							Usage:       "指定容量",
							Value:       20,
							Destination: &page_size,
						},
						cli.Int64Flag{
							Name:        "page, i",
							Usage:       "指定页码",
							Value:       1,
							Destination: &page,
						},
					},
				},
			},
		},
		{
			Name: "search",
			// Aliases:  []string{"a"},
			Usage:    "搜索",
			Category: "Basic Commands (Intermediate)",
			// Action:   getHandler,
			Subcommands: []cli.Command{
				{
					Name:   "repo",
					Usage:  "查询repo",
					Action: searchRepoHandler,
					Flags: []cli.Flag{
						cli.StringFlag{
							Name:     "project, p",
							Usage:    "指定项目名称",
							Required: true,
						},
					},
				},
				{
					Name:      "image",
					Usage:     "查询image",
					UsageText: "search image [repo_name] -t [tag]模糊查询 -p 项目",
					Action:    searchImageByTagsHandler,
					Flags: []cli.Flag{
						cli.StringFlag{
							Name:        "project, p",
							Usage:       "指定项目名称",
							Required:    true,
							Destination: &project,
						},
						cli.StringFlag{
							Name:        "tags, t",
							Usage:       "指定项目名称",
							Required:    true,
							Destination: &tags,
						},
						cli.Int64Flag{
							Name:        "pagesize, s",
							Usage:       "指定容量",
							Value:       20,
							Destination: &page_size,
						},
						cli.Int64Flag{
							Name:        "page, i",
							Usage:       "指定页码",
							Value:       1,
							Destination: &page,
						},
					},
				},
			},
		},
		{
			Name: "check",
			// Aliases:  []string{"a"},
			Usage:    "自检",
			Category: "Basic Commands (Intermediate)",
			Action:   checkHandler,
		},
		{
			Name: "login",
			// Aliases:  []string{"a"},
			Usage:    "登录",
			Category: "Basic Commands (Intermediate)",
			Action:   loginHandler,
		},
		{
			Name: "logout",
			// Aliases:  []string{"a"},
			Usage:    "退出",
			Category: "Basic Commands (Intermediate)",
			Action:   logoutHandler,
		},
	}
	sort.Sort(cli.FlagsByName(app.Flags))

	cli.HelpFlag = cli.BoolFlag{
		Name:  "help, h",
		Usage: "查看帮助信息",
	}

	cli.VersionFlag = cli.BoolFlag{
		Name:  "version, v",
		Usage: "查看版本信息",
	}

	app.Run(os.Args)
}
